package pod

import (
	"context"
	"krm_pro/config"

	"github.com/gin-gonic/gin"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type podInfo struct {
	PodName      string `json:"podname"`
	PodNamespace string `json:"podnamespace"`
	Status       string
	Nodename     string
	Image        string
	CreateTime   string
	Labels       map[string]string
	Pod          *corev1.Pod
}

var podSet = config.ClientSet.CoreV1().Pods

func PodList(r *gin.Context) {
	pods := podInfo{}
	ret := make([]*podInfo, 0)
	if err := r.ShouldBindJSON(&pods); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind fialed",
		})
		return
	}
	p := pods.list(pods.PodNamespace)
	for _, v := range p.Items {
		ret = append(ret, &podInfo{
			PodNamespace: v.Namespace,
			PodName:      v.Name,
			Status:       string(v.Status.Phase),
			Nodename:     v.Spec.NodeName,
			Image:        v.Spec.Containers[0].Image,
			CreateTime:   v.CreationTimestamp.Format("2006-01-02 15:04:05"),
		})
	}
	r.JSON(200, gin.H{
		"Pod": ret,
	})
}

func PodGet(r *gin.Context) {
	pod := podInfo{}
	if err := r.ShouldBindJSON(&pod); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind fialed",
		})
		return
	}
	p := pod.get(pod.PodName, pod.PodNamespace)
	r.JSON(200, gin.H{
		"status": 200,
		"pod":    p,
	})
}

func PodCreate(r *gin.Context) {
	pod := podInfo{}
	if err := r.ShouldBindJSON(&pod); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind fialed",
		})
		return
	}
	p := pod.create(pod.PodNamespace, pod.Pod)
	r.JSON(200, gin.H{
		"status": 200,
		"pod":    p,
	})
}

func PodUpdate(r *gin.Context) {
	pod := podInfo{}
	if err := r.ShouldBindJSON(&pod); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind fialed",
		})
		return
	}
	p := pod.update(pod.PodNamespace, pod.Pod)
	r.JSON(200, gin.H{
		"status": 200,
		"pod":    p,
	})
}

func PodDelete(r *gin.Context) {
	pod := podInfo{}
	if err := r.ShouldBindJSON(&pod); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind fialed",
		})
		return
	}
	pod.delete()
	r.JSON(200, gin.H{
		"status":  200,
		"message": "Pod Delete completed",
	})
}

func (p *podInfo) list(namespace string) *corev1.PodList {
	pods, err := podSet(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}
	return pods
}

func (p *podInfo) get(name string, namespace string) *corev1.Pod {
	pod, err := podSet(namespace).Get(context.TODO(), name, metav1.GetOptions{})
	if err != nil {
		panic(err.Error())
	}
	return pod
}

func (p *podInfo) create(namespace string, pod *corev1.Pod) *corev1.Pod {
	pod, err := podSet(namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
	if err != nil {
		panic(err.Error())
	}
	return pod
}

func (p *podInfo) update(namespace string, pod *corev1.Pod) *corev1.Pod {
	pod, err := podSet(namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
	if err != nil {
		panic(err.Error())
	}
	return pod
}
func (p *podInfo) delete() {
	err := podSet(p.PodNamespace).Delete(context.TODO(), p.PodName, metav1.DeleteOptions{})
	if err != nil {
		panic(err.Error())
	}
}
